package org.lex.ftp.ui;

import java.awt.BorderLayout;
import java.awt.Window;

import javax.swing.*;

public class MessageDialog extends JDialog {
	private JPanel pane = null;
	private JScrollPane scrollPane = null;
	private JTextArea textArea = null;
	private JPanel topPane = null;
	private JLabel messageLabel = null;
	private JCheckBox alwaysOnTopCheckBox = null;

	public MessageDialog() {
		super();
		initialize();
	}

	public MessageDialog(Window owner) {
		super(owner);
		initialize();
	}

	public void setPrompt(String prompt) {
		this.messageLabel.setText(prompt);
	}

	public String getMessage() {
		return this.getTextArea().getText();
	}

	public void setMessage(String message) {
		this.getTextArea().setText(message);
	}

	public void append(String message) {
		this.getTextArea().append(message);
	}

	public static void main(String[] args) {
		new MessageDialog().setVisible(true);
	}

	// =======================
	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getPane());
		this.setAlwaysOnTop(true);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getTopPane(), java.awt.BorderLayout.NORTH);
			pane.add(getScrollPane(), java.awt.BorderLayout.CENTER);
		}
		return pane;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	public JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}

	private JPanel getTopPane() {
		if (topPane == null) {
			messageLabel = new JLabel();
			messageLabel.setText("Input");
			topPane = new JPanel();
			topPane.setLayout(new BorderLayout());
			topPane.add(messageLabel, java.awt.BorderLayout.CENTER);
			topPane.add(getAlwaysOnTopCheckBox(), java.awt.BorderLayout.EAST);
		}
		return topPane;
	}

	private JCheckBox getAlwaysOnTopCheckBox() {
		if (alwaysOnTopCheckBox == null) {
			alwaysOnTopCheckBox = new JCheckBox();
			alwaysOnTopCheckBox.setText("On Top");
			alwaysOnTopCheckBox.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
			alwaysOnTopCheckBox.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
			alwaysOnTopCheckBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setAlwaysOnTop(alwaysOnTopCheckBox.isSelected());
				}
			});
			alwaysOnTopCheckBox.setSelected(true);
		}
		return alwaysOnTopCheckBox;
	}
}
